package vumt.radiocountries;

import java.util.Timer;
import java.util.TimerTask;

import vumt.radiocountries.adapter.CountryBaseAdapter;
import vumt.radiocountries.dialog.DialogInfoApp;
import vumt.radiocountries.dialog.DialogListRadioStation;
import vumt.radiocountries.dialog.DialogNoticeExitApp;
import vumt.radiocountries.util.ActionItem;
import vumt.radiocountries.util.Global;
import vumt.radiocountries.util.QuickAction;
import vumt.radiocountries.util.QuickAction.OnActionItemClickListener;
import vumt.radiocountries.util.QuickAction.OnDismissListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class MainActivity extends Activity implements OnClickListener,
		OnItemClickListener, TextWatcher, OnActionItemClickListener,
		OnDismissListener {
	private ListView listView;
	private Button btnMenu, btnClose;
	private EditText edSearch;
	private CountryBaseAdapter adapter;
	private QuickAction quickAction;
	private LinearLayout reLayout;
	private AdView adView;

	private static final int ID_INFO = 1;
	private static final int ID_FACEBOOK = 2;
	private static final int ID_TWITTER = 3;
	private static final int ID_MAIL = 4;
	private static final int ID_WEBSITE = 5;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		listView = (ListView) findViewById(R.id.listView);
		adapter = new CountryBaseAdapter(this, Global.listChannel);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(MainActivity.this);
		listView.setTextFilterEnabled(true);

		btnMenu = (Button) findViewById(R.id.btnMenu);
		btnMenu.setOnClickListener(this);
		btnClose = (Button) findViewById(R.id.btnClose);
		btnClose.setOnClickListener(this);

		edSearch = (EditText) findViewById(R.id.edSearch);
		edSearch.addTextChangedListener(this);

		quickAction = new QuickAction(this, QuickAction.ANIM_GROW_FROM_RIGHT);

		ActionItem itemInfo = new ActionItem(ID_INFO, Global.INFO_LABEL,
				getResources().getDrawable(R.drawable.img_info));
		ActionItem itemFB = new ActionItem(ID_FACEBOOK, Global.FB_LABEL,
				getResources().getDrawable(R.drawable.img_facebook));
		ActionItem itemTW = new ActionItem(ID_TWITTER, Global.TW_LABEL,
				getResources().getDrawable(R.drawable.img_twitter));
		ActionItem itemMail = new ActionItem(ID_MAIL, Global.MAIL_LABEL,
				getResources().getDrawable(R.drawable.img_mail));
		ActionItem itemWeb = new ActionItem(ID_WEBSITE, Global.WEB_LABEL,
				getResources().getDrawable(R.drawable.img_web));

		quickAction.addActionItem(itemInfo);
		quickAction.addActionItem(itemFB);
		quickAction.addActionItem(itemTW);
		quickAction.addActionItem(itemMail);
		quickAction.addActionItem(itemWeb);
		quickAction.setOnActionItemClickListener(this);
		quickAction.setOnDismissListener(this);

		reLayout = (LinearLayout) findViewById(R.id.reLayout);
 
		// ADMOB
		adView = (AdView) this.findViewById(R.id.ad);
 
		// Regza		("4E1F099A94A7FF2966392AEDC7C78FEB");
		// DOcomo       ("9B471B680AA37B3E3100BFDFFB75A258");
		// Galasy S     ("95B8C6D304285D123FDB9D6A56C5F10B");
		adView.loadAd(new AdRequest());
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		if (count < before)
			adapter.resetData();
		adapter.getFilter().filter(s.toString());
	}

	@Override
	public void afterTextChanged(Editable s) {
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		Intent i = new Intent(MainActivity.this, DialogListRadioStation.class);
		i.putExtra("position", position);
		startActivity(i);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnMenu:
			quickAction.show(v);
			break;
		case R.id.btnClose:
			TimerStart();
			break;

		default:
			break;
		}
	}

	private int time = 0;
	private Timer t;

	private void TimerStart() {
		reLayout.setVisibility(View.GONE);
		t = new Timer();
		t.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						time += 1;
						if (time > 10)
							TimerStop();
					}
				});
			}
		}, 0, 1000);
	}

	private void TimerStop() {
		reLayout.setVisibility(View.VISIBLE);
		time = 0;
		t.cancel();
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(getApplicationContext(),
				DialogNoticeExitApp.class);
		startActivityForResult(intent, 1);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		default:
			break;
		}
		if (resultCode == 2)
			finish();
	}

	@Override
	public void onItemClick(QuickAction source, int pos, int actionId) {
		switch (actionId) {
		case ID_INFO:
			Intent i = new Intent(MainActivity.this, DialogInfoApp.class);
			startActivity(i);
			break;

		default:
			break;
		}
	}

	@Override
	public void onDismiss() {
	}
}
